%     FASTENER DOCTOR - программа для лечения и конвертации крепежа
%     Copyright (C) 2018  Михаил Козлов
%     Сайт проекта: <http://www.fastenerdoctor.ru/>
%     Адрес электронной почты для связи с разработчиком:
%     <doctor.fastener@gmail.com>
% 
%     Эта программа является свободным программным обеспечением: Вы можете
%     распространять ее и/или изменять, соблюдая условия Универсальной общественной
%     лицензии GNU, опубликованной Фондом свободного программного обеспечения,
%     либо редакции 3, либо (на Ваше усмотрение) любой редакции, выпущенной позже.
% 
%     Эта программа распространяется в надежде на то, что она окажется полезной,
%     но БЕЗ КАКИХ-ЛИБО ГАРАНТИЙ, включая даже подразумеваемую гарантию ТОВАРНОЙ ПРИГОДНОСТИ,
%     либо ПРИГОДНОСТИ ДЛЯ ОПРЕДЕЛЕННЫХ ЦЕЛЕЙ. Ознакомьтесь с Универсальной общественной
%     лицензией GNU для получения более подробной информации.
% 
%     Вы должны были получить копию Универсальной общественной лицензии GNU
%     вместе с этой программой. Если Вы ее не получили, то перейдите по адресу:
%     <http://www.gnu.org/licenses/>.
% 
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <https://www.gnu.org/licenses/>.

function DB_PSETS = Create_DB_PSETS(PSETS)

% До обработки:

% PSETS (карточка  PSOLID) | .card | .ID | .Patran_name | .Mat_ID | .CORDM | .IN | .STRESS | .ISOP | .FCTN | .COROT |
% PSETS (карточка   PCOMP) | .card | .ID | .Patran_name | .Patran_laminate_name | .Z0 | .NSM | .SB | .FT | .TREF | .GE | .LAM | .N_plies | .Laminate(N_plies,4) |
%           .Laminate(i,4) | MIDi | Ti | THETAi | SOUTi (1 = YES, 0 = NO) |
% PSETS (карточка  PCOMPG) | .card | .ID | .Patran_name | .Patran_laminate_name | .Z0 | .NSM | .SB | .FT | .TREF | .GE | .LAM | .N_plies | .Laminate(N_plies,5) | .GEFLG |
%           .Laminate(i,5) | GPLYIDi | MIDi | Ti | THETAi | SOUTi (1 = YES, 0 = NO) |
% PSETS (карточка  PSHELL) | .card | .ID | .Patran_name | .MID1 | .T | .MID2 | .I_ratio | .MID3 | .TS_T | .NSM | .Z1 | .Z2 | .MID4 |
% PSETS (карточка   PBUSH) | .card | .ID | .Patran_name | .K(6,1) | .B(6,1) | .GE(6,1) | .RCV(.SA .ST .EA .ET) | .M |
% PSETS (карточка    PROD) | .card | .ID | .Patran_name | .MID | .A | .J | .C | .NSM |
% PSETS (карточка PBMSECT) | .card | .ID | .Patran_name | .MID | .FORM | .Section_data |
% PSETS (карточка PBRSECT) | .card | .ID | .Patran_name | .MID | .FORM | .NSM | .Section_data |
% PSETS (карточка    PBAR) | .card | .ID | .Patran_name | .MID | .A | .I1 | .I2 | .J | .NSM | .C1 | .C2 | .D1 | .D2 | .E1 | .E2 | .F1 | .F2 | .K1 | .K2 | .I12 |
% PSETS (карточка   PBARL) | .card | .ID | .Patran_name | .MID | .GROUP | .TYPE | .DIM(:,1)| .NSM |
%                   .TYPE = (ROD, TUBE, TUBE2, L, I, CHAN, T, BOX, BAR, CROSS, H, T1, I1, CHAN1, Z, CHAN2, T2, BOX1, HEXA, HAT, HAT1, DBOX )
% PSETS (карточка   PBEAM) | .card | .ID | .Patran_name | .MID | .A | .I1 | .I2 | .I12 | .J | .NSM | .C1 | .C2 | .D1 | .D2 | .E1 | .E2 | .F1 | .F2 |
%                          | .N_intermediate_states | .Intermediate_states_data{N_intermediate_states,1} |
%                          | .K1 | .K2 | .S1 | .S2 | .NSI_A | .NSI_B | .CW_A | .CW_B | .M1_A | .M2_A | .M1_B | .M2_B | .N1_A | .N2_A | .N1_B | .N2_B |
%             .Intermediate_states_data{i} : { .SO | .X_XB | .A | .I1 | .I2 | .I12 | .J | .NSM | .C1 | .C2 | .D1 | .D2 | .E1 | .E2 | .F1 | .F2 }
% PSETS (карточка  PBEAML) | .card | .ID | .Patran_name | .MID | .GROUP | .TYPE | .DIM(:,1)| .NSM | .N_intermediate_states | .Intermediate_states_data{N_intermediate_states,1} |
%             .Intermediate_states_data{i} : { .SO | .X_XB | .DIM(:,1)| .NSM }
%                   .TYPE = (ROD, TUBE, TUBE2, L, I, CHAN, T, BOX, BAR, CROSS, H, T1, I1, CHAN1, Z, CHAN2, T2, BOX1, HEXA, HAT, HAT1, DBOX )
% PSETS (карточка  PBCOMP) | .card | .ID | .Patran_name | .MID | .A | .I1 | .I2 | .I12 | .J | .NSM | .K1 | .K2 | .M1 | .M2 | .N1 | .N2 | .SYMOPT | .N_lumped_areas | .Lumped_area_data{N_lumped_areas,1} |
%             .Lumped_area_data{i} : { .Y | .Z | .C | .MID }


% После обработки:

% DB_PSETS ( PSOLID) | .type | .ID | .Patran_name | .Mat_ID | .Ori_CID (-1, если element coordinate system) |
% DB_PSETS ( PCOMP)  | .type | .ID | .Patran_name | .Patran_laminate_name | .N_plies | .Laminate(:,3) | .Laminate_offset |
%                    | .Ori_Field_ID | .Offset_Field_ID |         DB_FIELDS{Ori_Field_ID}.Value_data(:,2) [Value_data(j,1) - ori_type: (0 - CID | 1 - Angle)] [Value_data(j,2) - ori_data]
%             .Laminate(i,3) | Mat_ID_i | t_i | Theta_i |
% DB_PSETS ( PCOMPG) | .type | .ID | .Patran_name | .Patran_laminate_name | .N_plies | .Laminate(:,4) | .Laminate_offset |
%                    | .Ori_Field_ID | .Offset_Field_ID |         DB_FIELDS{Ori_Field_ID}.Value_data(:,2) [Value_data(j,1) - ori_type: (0 - CID | 1 - Angle)] [Value_data(j,2) - ori_data]
%             .Laminate(i,4) | Mat_ID_i | t_i | Theta_i | Global_ply_ID_i |
% DB_PSETS ( PSHELL) | .type | .ID | .Patran_name | .Mat_ID_membrane | .Mat_ID_bending | .Mat_ID_transverse_shear | .t |
%                    | .Ori_Field_ID | .Offset_Field_ID |         DB_FIELDS{Ori_Field_ID}.Value_data(:,2) [Value_data(j,1) - ori_type: (0 - CID | 1 - Angle)] [Value_data(j,2) - ori_data]
% DB_PSETS (  PBUSH) | .type | .ID | .Patran_name | .K(6,1) | .Ori_CID_Field_ID |
% DB_PSETS (   PROD) | .type | .ID | .Patran_name | .Mat_ID | .Area | .J |
% DB_PSETS (PBMSECT) | .type | .ID | .Patran_name | .Mat_ID | .Cross_section_form | .Section_data |
%                    | .Ori_vector_Field_ID | .Offset_vector_A_Field_ID | .Offset_vector_B_Field_ID | .Pinned_DOFs_A_Field_ID | .Pinned_DOFs_B_Field_ID |     (вектора ориентации и офсетов в глобальной СК)
% DB_PSETS (PBRSECT) | .type | .ID | .Patran_name | .Mat_ID | .Cross_section_form | .Section_data |
%                    | .Ori_vector_Field_ID | .Offset_vector_A_Field_ID | .Offset_vector_B_Field_ID | .Pinned_DOFs_A_Field_ID | .Pinned_DOFs_B_Field_ID |     (вектора ориентации и офсетов в глобальной СК)
% DB_PSETS (   PBAR) | .type | .ID | .Patran_name | .Mat_ID | .Area | .I1 | .I2 | .I12 | .J | .K1 | .K2 |
%                    | .Ori_vector_Field_ID | .Offset_vector_A_Field_ID | .Offset_vector_B_Field_ID | .Pinned_DOFs_A_Field_ID | .Pinned_DOFs_B_Field_ID |     (вектора ориентации и офсетов в глобальной СК)
% DB_PSETS (  PBARL) | .type | .ID | .Patran_name | .Mat_ID | .Shape | .Dim(:,1) |
%                    | .Ori_vector_Field_ID | .Offset_vector_A_Field_ID | .Offset_vector_B_Field_ID | .Pinned_DOFs_A_Field_ID | .Pinned_DOFs_B_Field_ID |     (вектора ориентации и офсетов в глобальной СК)
%             .Shape = (ROD, TUBE, TUBE2, L, I, CHAN, T, BOX, BAR, CROSS, H, T1, I1, CHAN1, Z, CHAN2, T2, BOX1, HEXA, HAT, HAT1, DBOX )
% DB_PSETS (  PBEAM) | .type | .ID | .Patran_name | .Mat_ID | .Area | .I1 | .I2 | .I12 | .J | .K1 | .K2 | .S1 | .S2 | .N1_A | .N2_A | .N1_B | .N2_B |
%                    | .N_intermediate_states | .Intermediate_states_data{N_intermediate_states,1} |
%                    | .Ori_vector_Field_ID | .Offset_vector_A_Field_ID | .Offset_vector_B_Field_ID | .Pinned_DOFs_A_Field_ID | .Pinned_DOFs_B_Field_ID |     (вектора ориентации и офсетов в глобальной СК)
%             .Intermediate_states_data{i} : { .Location | .A | .I1 | .I2 | .I12 | .J }
% DB_PSETS ( PBEAML) | .type | .ID | .Patran_name | .Mat_ID | .Shape | .Dim(:,1) | .N_intermediate_states | .Intermediate_states_data{N_intermediate_states,1} |
%                    | .Ori_vector_Field_ID | .Offset_vector_A_Field_ID | .Offset_vector_B_Field_ID | .Pinned_DOFs_A_Field_ID | .Pinned_DOFs_B_Field_ID |     (вектора ориентации и офсетов в глобальной СК)
%             .Intermediate_states_data{i} : { .Location | .Dim(:,1) }
%             .Shape = (ROD, TUBE, TUBE2, L, I, CHAN, T, BOX, BAR, CROSS, H, T1, I1, CHAN1, Z, CHAN2, T2, BOX1, HEXA, HAT, HAT1, DBOX )
% DB_PSETS ( PBCOMP) | .type | .ID | .Patran_name | .Mat_ID | .Area | .I1 | .I2 | .I12 | .J | .K1 | .K2 | .N1 | .N2 | .SYMOPT | .N_lumped_areas | .Lumped_area_data{N_lumped_areas,1} |
%                    | .Ori_vector_Field_ID | .Offset_vector_A_Field_ID | .Offset_vector_B_Field_ID | .Pinned_DOFs_A_Field_ID | .Pinned_DOFs_B_Field_ID |     (вектора ориентации и офсетов в глобальной СК)
%             .Lumped_area_data{i} : { .Y | .Z | .C | .MID }


global ErrCode;
global Laminate_offset_round_tolerance;

fprintf (1, 'Обработка property sets...              ');

DB_PSETS = cell(size(PSETS,1),1);

for i=1:size(PSETS,1)
    
    switch PSETS{i}.card
        
        case 'PSOLID'
            
            DB_PSETS{i}.type = PSETS{i}.card;
            DB_PSETS{i}.ID = PSETS{i}.ID;
            DB_PSETS{i}.Patran_name = PSETS{i}.Patran_name;
            DB_PSETS{i}.Mat_ID = PSETS{i}.Mat_ID;
            DB_PSETS{i}.Ori_CID = PSETS{i}.CORDM;
                        
        case 'PCOMP'
            
            DB_PSETS{i}.type = PSETS{i}.card;
            DB_PSETS{i}.ID = PSETS{i}.ID;
            DB_PSETS{i}.Patran_name = PSETS{i}.Patran_name;
            DB_PSETS{i}.Patran_laminate_name = PSETS{i}.Patran_laminate_name;
            DB_PSETS{i}.N_plies = PSETS{i}.N_plies;
            
            DB_PSETS{i}.Laminate = zeros(PSETS{i}.N_plies,3);
            
            DB_PSETS{i}.Laminate(:,1) = PSETS{i}.Laminate(:,1);
            DB_PSETS{i}.Laminate(:,2) = PSETS{i}.Laminate(:,2);
            DB_PSETS{i}.Laminate(:,3) = PSETS{i}.Laminate(:,3);
            
            if (isempty(PSETS{i}.Z0))
                DB_PSETS{i}.Laminate_offset = 0;
            else
                Lam_thickness = 0;
                for j=1:PSETS{i}.N_plies
                    Lam_thickness = Lam_thickness + PSETS{i}.Laminate(j,2);
                end
                DB_PSETS{i}.Laminate_offset = Lam_thickness/2 + PSETS{i}.Z0;
                if (abs(DB_PSETS{i}.Laminate_offset) <= Laminate_offset_round_tolerance)
                    DB_PSETS{i}.Laminate_offset = 0;
                end
            end
            
            DB_PSETS{i}.Ori_Field_ID = DB_Field_Create(2);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Offset_Field_ID = DB_Field_Create(1);
            if (ErrCode < 0)
                return;
            end
            
        case 'PCOMPG'
            
            DB_PSETS{i}.type = PSETS{i}.card;
            DB_PSETS{i}.ID = PSETS{i}.ID;
            DB_PSETS{i}.Patran_name = PSETS{i}.Patran_name;
            DB_PSETS{i}.Patran_laminate_name = PSETS{i}.Patran_laminate_name;
            DB_PSETS{i}.N_plies = PSETS{i}.N_plies;
            
            DB_PSETS{i}.Laminate = zeros(PSETS{i}.N_plies,4);
            
            DB_PSETS{i}.Laminate(:,1) = PSETS{i}.Laminate(:,2);
            DB_PSETS{i}.Laminate(:,2) = PSETS{i}.Laminate(:,3);
            DB_PSETS{i}.Laminate(:,3) = PSETS{i}.Laminate(:,4);
            DB_PSETS{i}.Laminate(:,4) = PSETS{i}.Laminate(:,1);
            
            if (isempty(PSETS{i}.Z0))
                DB_PSETS{i}.Laminate_offset = 0;
            else
                Lam_thickness = 0;
                for j=1:PSETS{i}.N_plies
                    Lam_thickness = Lam_thickness + PSETS{i}.Laminate(j,3);
                end
                DB_PSETS{i}.Laminate_offset = Lam_thickness/2 + PSETS{i}.Z0;
                if (abs(DB_PSETS{i}.Laminate_offset) < Laminate_offset_round_tolerance)
                    DB_PSETS{i}.Laminate_offset = 0;
                end
            end
            
            DB_PSETS{i}.Ori_Field_ID = DB_Field_Create(2);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Offset_Field_ID = DB_Field_Create(1);
            if (ErrCode < 0)
                return;
            end
            
        case 'PSHELL'
            
            DB_PSETS{i}.type = PSETS{i}.card;
            DB_PSETS{i}.ID = PSETS{i}.ID;
            DB_PSETS{i}.Patran_name = PSETS{i}.Patran_name;
            DB_PSETS{i}.Mat_ID_membrane = PSETS{i}.MID1;
            DB_PSETS{i}.Mat_ID_bending = PSETS{i}.MID2;
            DB_PSETS{i}.Mat_ID_transverse_shear = PSETS{i}.MID3;
            DB_PSETS{i}.t = PSETS{i}.T;
            
            DB_PSETS{i}.Ori_Field_ID = DB_Field_Create(2);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Offset_Field_ID = DB_Field_Create(1);
            if (ErrCode < 0)
                return;
            end
            
        case 'PBUSH'
            
            DB_PSETS{i}.type = PSETS{i}.card;
            DB_PSETS{i}.ID = PSETS{i}.ID;
            DB_PSETS{i}.Patran_name = PSETS{i}.Patran_name;
            DB_PSETS{i}.K = PSETS{i}.K;
            
            DB_PSETS{i}.Ori_CID_Field_ID = DB_Field_Create(1);
            if (ErrCode < 0)
                return;
            end
            
        case 'PROD'
            
            DB_PSETS{i}.type = PSETS{i}.card;
            DB_PSETS{i}.ID = PSETS{i}.ID;
            DB_PSETS{i}.Patran_name = PSETS{i}.Patran_name;
            DB_PSETS{i}.Mat_ID = PSETS{i}.MID;
            DB_PSETS{i}.Area = PSETS{i}.A;
            DB_PSETS{i}.J = [];
            
            if (~isempty(PSETS{i}.J))
                DB_PSETS{i}.J = PSETS{i}.J;
            end
            
        case 'PBMSECT'
            
            DB_PSETS{i}.type = PSETS{i}.card;
            DB_PSETS{i}.ID = PSETS{i}.ID;
            DB_PSETS{i}.Patran_name = PSETS{i}.Patran_name;
            DB_PSETS{i}.Mat_ID = PSETS{i}.MID;
            DB_PSETS{i}.Cross_section_form = PSETS{i}.FORM;
            DB_PSETS{i}.Section_data = PSETS{i}.Section_data;
            
            DB_PSETS{i}.Ori_vector_Field_ID = DB_Field_Create(3);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Offset_vector_A_Field_ID = DB_Field_Create(3);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Offset_vector_B_Field_ID = DB_Field_Create(3);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Pinned_DOFs_A_Field_ID = DB_Field_Create(1);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Pinned_DOFs_B_Field_ID = DB_Field_Create(1);
            if (ErrCode < 0)
                return;
            end
            
        case 'PBRSECT'
            
            DB_PSETS{i}.type = PSETS{i}.card;
            DB_PSETS{i}.ID = PSETS{i}.ID;
            DB_PSETS{i}.Patran_name = PSETS{i}.Patran_name;
            DB_PSETS{i}.Mat_ID = PSETS{i}.MID;
            DB_PSETS{i}.Cross_section_form = PSETS{i}.FORM;
            DB_PSETS{i}.Section_data = PSETS{i}.Section_data;
            
            DB_PSETS{i}.Ori_vector_Field_ID = DB_Field_Create(3);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Offset_vector_A_Field_ID = DB_Field_Create(3);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Offset_vector_B_Field_ID = DB_Field_Create(3);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Pinned_DOFs_A_Field_ID = DB_Field_Create(1);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Pinned_DOFs_B_Field_ID = DB_Field_Create(1);
            if (ErrCode < 0)
                return;
            end
            
        case 'PBAR'
            
            DB_PSETS{i}.type = PSETS{i}.card;
            DB_PSETS{i}.ID = PSETS{i}.ID;
            DB_PSETS{i}.Patran_name = PSETS{i}.Patran_name;
            DB_PSETS{i}.Mat_ID = PSETS{i}.MID;
            DB_PSETS{i}.Area = PSETS{i}.A;
            DB_PSETS{i}.I1 = PSETS{i}.I1;
            DB_PSETS{i}.I2 = PSETS{i}.I2;
            
            if (~isempty(PSETS{i}.I12))
                DB_PSETS{i}.I12 = PSETS{i}.I12;
            else
                DB_PSETS{i}.I12 = 0;
            end
            
            DB_PSETS{i}.J = PSETS{i}.J;
            DB_PSETS{i}.K1 = PSETS{i}.K1;
            DB_PSETS{i}.K2 = PSETS{i}.K2;
            
            DB_PSETS{i}.Ori_vector_Field_ID = DB_Field_Create(3);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Offset_vector_A_Field_ID = DB_Field_Create(3);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Offset_vector_B_Field_ID = DB_Field_Create(3);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Pinned_DOFs_A_Field_ID = DB_Field_Create(1);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Pinned_DOFs_B_Field_ID = DB_Field_Create(1);
            if (ErrCode < 0)
                return;
            end
            
        case 'PBARL'
            
            DB_PSETS{i}.type = PSETS{i}.card;
            DB_PSETS{i}.ID = PSETS{i}.ID;
            DB_PSETS{i}.Patran_name = PSETS{i}.Patran_name;
            DB_PSETS{i}.Mat_ID = PSETS{i}.MID;
            DB_PSETS{i}.Shape = PSETS{i}.TYPE;
            DB_PSETS{i}.Dim = PSETS{i}.DIM;
            
            DB_PSETS{i}.Ori_vector_Field_ID = DB_Field_Create(3);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Offset_vector_A_Field_ID = DB_Field_Create(3);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Offset_vector_B_Field_ID = DB_Field_Create(3);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Pinned_DOFs_A_Field_ID = DB_Field_Create(1);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Pinned_DOFs_B_Field_ID = DB_Field_Create(1);
            if (ErrCode < 0)
                return;
            end
            
        case 'PBEAM'
            
            DB_PSETS{i}.type = PSETS{i}.card;
            DB_PSETS{i}.ID = PSETS{i}.ID;
            DB_PSETS{i}.Patran_name = PSETS{i}.Patran_name;
            DB_PSETS{i}.Mat_ID = PSETS{i}.MID;
            DB_PSETS{i}.Area = PSETS{i}.A;
            DB_PSETS{i}.I1 = PSETS{i}.I1;
            DB_PSETS{i}.I2 = PSETS{i}.I2;
            
            if (~isempty(PSETS{i}.I12))
                DB_PSETS{i}.I12 = PSETS{i}.I12;
            else
                DB_PSETS{i}.I12 = 0;
            end
            
            DB_PSETS{i}.J = PSETS{i}.J;
            DB_PSETS{i}.K1 = PSETS{i}.K1;
            DB_PSETS{i}.K2 = PSETS{i}.K2;
            DB_PSETS{i}.S1 = PSETS{i}.S1;
            DB_PSETS{i}.S2 = PSETS{i}.S2;
            DB_PSETS{i}.N1_A = PSETS{i}.N1_A;
            DB_PSETS{i}.N2_A = PSETS{i}.N2_A;
            DB_PSETS{i}.N1_B = PSETS{i}.N1_B;
            DB_PSETS{i}.N2_B = PSETS{i}.N2_B;
            
            DB_PSETS{i}.N_intermediate_states = PSETS{i}.N_intermediate_states;
            DB_PSETS{i}.Intermediate_states_data = [];
            
            if (PSETS{i}.N_intermediate_states > 0)
                
                DB_PSETS{i}.Intermediate_states_data = cell(PSETS{i}.N_intermediate_states,1);

                for k=1:PSETS{i}.N_intermediate_states

                    DB_PSETS{i}.Intermediate_states_data{k}.Location = PSETS{i}.Intermediate_states_data{k}.X_XB;
                    DB_PSETS{i}.Intermediate_states_data{k}.A = PSETS{i}.Intermediate_states_data{k}.A;
                    DB_PSETS{i}.Intermediate_states_data{k}.I1 = PSETS{i}.Intermediate_states_data{k}.I1;
                    DB_PSETS{i}.Intermediate_states_data{k}.I2 = PSETS{i}.Intermediate_states_data{k}.I2;
                    DB_PSETS{i}.Intermediate_states_data{k}.I12 = PSETS{i}.Intermediate_states_data{k}.I12;
                    DB_PSETS{i}.Intermediate_states_data{k}.J = PSETS{i}.Intermediate_states_data{k}.J;

                end
            
            end
            
            DB_PSETS{i}.Ori_vector_Field_ID = DB_Field_Create(3);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Offset_vector_A_Field_ID = DB_Field_Create(3);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Offset_vector_B_Field_ID = DB_Field_Create(3);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Pinned_DOFs_A_Field_ID = DB_Field_Create(1);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Pinned_DOFs_B_Field_ID = DB_Field_Create(1);
            if (ErrCode < 0)
                return;
            end
            
        case 'PBEAML'
            
            DB_PSETS{i}.type = PSETS{i}.card;
            DB_PSETS{i}.ID = PSETS{i}.ID;
            DB_PSETS{i}.Patran_name = PSETS{i}.Patran_name;
            DB_PSETS{i}.Mat_ID = PSETS{i}.MID;
            DB_PSETS{i}.Shape = PSETS{i}.TYPE;
            DB_PSETS{i}.Dim = PSETS{i}.DIM;
            
            DB_PSETS{i}.N_intermediate_states = PSETS{i}.N_intermediate_states;
            DB_PSETS{i}.Intermediate_states_data = [];
            
            if (PSETS{i}.N_intermediate_states > 0)
                
                DB_PSETS{i}.Intermediate_states_data = cell(PSETS{i}.N_intermediate_states,1);

                for k=1:PSETS{i}.N_intermediate_states

                    DB_PSETS{i}.Intermediate_states_data{k}.Location = PSETS{i}.Intermediate_states_data{k}.X_XB;
                    DB_PSETS{i}.Intermediate_states_data{k}.Dim = PSETS{i}.Intermediate_states_data{k}.DIM;

                end
            
            end
            
            DB_PSETS{i}.Ori_vector_Field_ID = DB_Field_Create(3);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Offset_vector_A_Field_ID = DB_Field_Create(3);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Offset_vector_B_Field_ID = DB_Field_Create(3);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Pinned_DOFs_A_Field_ID = DB_Field_Create(1);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Pinned_DOFs_B_Field_ID = DB_Field_Create(1);
            if (ErrCode < 0)
                return;
            end
            
        case 'PBCOMP'
            
            DB_PSETS{i}.type = PSETS{i}.card;
            DB_PSETS{i}.ID = PSETS{i}.ID;
            DB_PSETS{i}.Patran_name = PSETS{i}.Patran_name;
            DB_PSETS{i}.Mat_ID = PSETS{i}.MID;
            DB_PSETS{i}.Area = PSETS{i}.A;
            DB_PSETS{i}.I1 = PSETS{i}.I1;
            DB_PSETS{i}.I2 = PSETS{i}.I2;
            DB_PSETS{i}.I12 = PSETS{i}.I12;
            DB_PSETS{i}.J = PSETS{i}.J;
            DB_PSETS{i}.K1 = PSETS{i}.K1;
            DB_PSETS{i}.K2 = PSETS{i}.K2;
            DB_PSETS{i}.N1 = PSETS{i}.N1;
            DB_PSETS{i}.N2 = PSETS{i}.N2;
            DB_PSETS{i}.SYMOPT = PSETS{i}.SYMOPT;
            
            DB_PSETS{i}.N_lumped_areas = PSETS{i}.N_lumped_areas;
            DB_PSETS{i}.Lumped_area_data = [];
            
            if (PSETS{i}.N_lumped_areas > 0)
                
                DB_PSETS{i}.Lumped_area_data = cell(PSETS{i}.N_lumped_areas,1);

                for k=1:PSETS{i}.N_lumped_areas
                    DB_PSETS{i}.Lumped_area_data(k) = PSETS{i}.Lumped_area_data(k);
                end
            
            end
            
            DB_PSETS{i}.Ori_vector_Field_ID = DB_Field_Create(3);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Offset_vector_A_Field_ID = DB_Field_Create(3);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Offset_vector_B_Field_ID = DB_Field_Create(3);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Pinned_DOFs_A_Field_ID = DB_Field_Create(1);
            if (ErrCode < 0)
                return;
            end
            
            DB_PSETS{i}.Pinned_DOFs_B_Field_ID = DB_Field_Create(1);
            if (ErrCode < 0)
                return;
            end
            
        otherwise
            
            fprintf (1, '\nНеизвестная карточка property set %i [%s] !\n', PSETS{i}.ID, PSETS{i}.card);
            ErrCode = -1;
            return;
        
    end
    
end

fprintf (1, 'OK!\n');
